home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Technotools
/
Technotools (Chestnut CD-ROM)(1993).ISO
/
misc_pto
/
oof
/
oof.doc
< prev
next >
Wrap
Text File
|
1987-09-19
|
9KB
|
216 lines
{ Version 2.00 87/09/19
OOF! (Object Oriented Fudge)
Utilities for Object Oriented Programming in TURBO Pascal.
COPYRIGHT NOTICE:
These utilities are COPYRIGHT (c) 1987 by Mike Babulic. They may be used
as if they were in the public domain so long as the following conditions are
met:
1) This notice of copyright must be included in full unless the
copyright owner's permission is obtained.
2) Commercial publishers must obtain the copyright owner's permission
before physically publishing this work. Anyone may publish this
work in a non-physical form.
- some examples of non-physical publication (allowed):
- Electronic Bulletin Boards, Compuserve
- some examples of physical publication (prohibited):
- Books, Magazines, Floppy Disks
- Computer clubs are NOT considered to be commercial publishers
AUTHOR: Mike Babulic
3827 Charleswood Drive N.W.
Calgary, Alberta
CANADA
T2L 2C7
Compuserve Id.: 72307,314
A SMALL TALK (pun intended):
These utilities "Fudge things", extending TURBO Pascal to allow an
object-oriented approach to programming.
Object-oriented programming is neet stuff! Each OBJECT is a member of a
CLASS. A class is simaler to a TYPE in ordinary Pascal but it is also
more! A class is a type and all the operations that can be performed on
that type. These operations are called a class's METHODS.
A CLASS consists of both a datastructure and the methods (procedures and
functions) which operate on that datastructure.
To perform an operation, you send a MESSAGE containing ARGUMENTS to the
object that you wish to use. In the case of these utilities a message is a
procedure call whose last argument is the object.
So far this is quite unexciting. It appears to be a complicated way of
talking about things that could be explained more simply. All this jargon
has a purpose however, it allows us to discuss the concept that makes the
object-oriented approach so powerful. That concept is INHERITANCE.
When a class of objects is being defined, it can INHERIT the properties of
a PARENT class. Exceptions and additions can then be described. In other
words you can say things like "This class is just like the previous one
except that it should also have a new data field and the procedure to
write it on the screen should be changed". That has the potential of saving
a LOT of coding!
Here is another advantage: suppose you have a program that manipulates
various shapes. A piece of code to draw these shapes on the screen might
look something like this:
CASE thing.kind of
point: drawPoint(thing);
circle: drawCircle(thing);
triangle: drawTriang(thing);
and so on ...
A chunk of object oriented fudge might look like this:
DrawShape(thing);
If you add other shapes (squares, pentagons, etc.) you will have to change
CASE statements throughout your program to account for them. Nothing needs
changing in the object-oriented program!
WHAT'S THE DIFFERENCE BETWEEN Object AND Instance?
The same as the difference between TYPE and VARIABLE.
- An OBJECT is a DEFINITION (datastructure, messages and methods).
- An INSTANCE is a THING WHO'S DEFINITION IS AN OBJECT.
In other words: Objects are like expanded type declarations.
Object Variables are "instances" of objects.
IMPLEMENTATION DECISIONS
- The MESSAGE RECEIVER (object or class) is a VAR parameter and
the LAST PARAMETER of a message procedure.
1) VAR PARAMETER - an object's data may be changed by a method.
2) LAST PARAMETER - the object is easy to find on the stack.
It is always second from the top under the return address
so the dispatcher doesn't need to know how many
parameters a method has.
- A CLASS is represented as the address (ie. offset from CSeg) of the
dispatcher procedure of that class. (reasons: simplicity & speed)
- METHODS are "FORWARD" procedures.
One of the quirks of Turbo Pascal is that a forward procedure
declaration is complied as a JMP to the second declaration, which
follows later in the code segment.
Declaring the methods of an object type as "forward" and putting all
these declarations together in one place means that we create an
ARRAY OF PROCEDURES. This makes it easy for the dispatcher procedure
to select the correct method when passed a message.
Each JMP is 3 bytes long. That is why messages are numbered 0,3,9...
instead of 1,2,3...
- OOF uses the STACK and NOT THE HEAP to store instances of objects.
This is unlike most object oriented programming systems. They
are almost always heap based.
Why is OOF stack based? Well, I'm not just being ornery ...
1) Garbage collection - this is trivial (and extremely fast)
with stack-based objects.
2) Safety - a programmer using Object Pascal must dispose of
(and C++ programmers must free) instances of objects when
they are finished with them. This means there is a danger of
DANGLING POINTERS.
3) Appropriate Model - the vast majority of object instances are
created by the method that uses them. Why complicate things
with both an object handle in the stack AND an instance in
the heap?
NOTATIONAL CONVENTIONS (adapted from Object Pascal)
The TYPE of object is called "T<name>".
- example: TWindow
The METHODS of an object all have names beginning with the name of the
TYPE, followed by an underscore.
-example: TWindow_WTitle
The DISPATCHER procedure of an object is called "C<name>" because
it's address (offset) defines the class.
- example: PROCEDURE CWindow(message,number:Integer);
The LAST PARAMETER of a MESSAGE is a typeless VAR parameter which
represents the object or class the message is to be passed to.
It should be called "SELF" unless the message is a class message;
then it should be called "aClass".
The LAST PARAMETER of a METHOD is a VAR parameter of method's TYPE.
It should be called "SELF" unless the message is a class message;
then it should be called "aClass".
HOW TO USE OOF / DEMO PROGRAM
OOF.INC is an $Include file that implements the object programming
extensions to Turbo Pascal
DEMO.PAS shows you how to organize your program to do object oriented
programming.
*** IMPORTANT ***
MESSAGES are numbered 0,3,6,9 ... etc. NOT 1,2,3
METHODS must be declared as a LIST OF FORWARD PROCEDURES.
There must be nothing but comments between methods
in the list (no constants, no variables, no other
procedures or functions ... none! nothing! nada!)
PAST & FUTURE:
I got the idea for this stuff from Object Pascal, which I first encountered
on Apple's Macintosh computer. A look through "SMALLTALK-80, The Language
and Its Implementation" by Goldberg & Robson (Addison-Wesley) also helped
me to understand object-oriented programming.
I hope to use this Object Oriented Fudge to create a window manager for
turbo programmers. It should be just the sort of project to build a
"wish list" for version 3 and also to shake out any problems there might be
in version 2.
If you have any suggestions or comments, please write or send me Compuserve
EMAIL. I also log on to a friend's BBS quite often & can usually be reached
there (Calgary FIDO: (403) 282-1703, net/node = 134/1).
BIBLEOGRAPHY:
1) "SMALLTALK-80, The Language and Its Implementation"
Goldberg & Robson (Addison-Wesley)
2) "Object-Oriented Programming for the Macintosh"
Kurt J. Schmucker (Hayden, 1986)
3) "TML Pascal User's Guide and Reference Manual"
(TML Systems, 1986)
4) "Object Oriented Programming, An Evolutionary Approach"
Brad J. Cox (Addison-Wesley, 1986)